<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/12
 * Time: 16:48
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Activity  extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.activity_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($activity_type)&&$activity_type!="")
            {
                $map['a.activity_type'] = $activity_type;
            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
//            {
//                $map['a.activity_time'] = ['>= time',$start];
//            }
//            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
//            {
//                $map['a.activity_time'] = ['<= time',$end];
//            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
//            {
//                $map['a.activity_time'] = ['between time',[$start,$end]];
//            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('activity')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('activity')->alias('a')
                ->where($map)
                ->field('a.*')
                ->page($NowPage,$limits)
                ->order('activity_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $activity_title = trim(input('notice_title'), ' ');
            $activity_img = input('notice_thumb');
//            $start = input('start');
//            $end = input('end');
//            if(strtotime($end) <= strtotime($start)){
//                return json(['code' => 100, 'msg' => '结束时间不得早于开始时间']);
//            }
            $activity_content = input('notice_content');
            $data = [
                'activity_title' => $activity_title,
                'activity_img' => $activity_img,
                'activity_time' => date('Y-m-d H:i:s'),
//                'activity_start_time' => $start,
//                'activity_end_time' => $end,
                'activity_content'=>$activity_content,
            ];
            $res = Db::table('up_activity')->insertGetId($data);
            if($res){
                // 消息
                $notice = [
                    'notice_type' => 2,
                    'notice_cate' => 2,
                    'notice_subject_id' => $res,
                    'notice_title' => $activity_title,
                    'notice_user' => -1,
                    'notice_read_user' => ',0,',
                    'notice_del_user' => ',0,',
                    'notice_time' => date('Y-m-d H:i:s'),
                ];
                Db::table('up_notice')->insert($notice);
                // 推送
                try{
                    $this->Jpush(2, '', $activity_title, ['send_type' => 6, 'activity_id' => $res]);
                }catch(\Exception $e){
        
                }
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $activity_title = trim(input('notice_title'), ' ');
//            $start = input('start');
//            $end = input('end');
//            if(strtotime($end) <= strtotime($start)){
//                return json(['code' => 100, 'msg' => '结束时间不得早于开始时间']);
//            }
            $activity_content = input('notice_content');
            $data['activity_title'] = $activity_title;
//            $data['activity_start_time'] = $start;
//            $data['activity_end_time'] = $end;
            $data['activity_content'] = $activity_content;
            if(!empty(input('notice_thumb'))){
                $data['activity_img'] = input('notice_thumb');
            }

            $res = Db::table('up_activity')->where('activity_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $activity_id = input('param.activity_id');
        $info = Db::table('up_activity')->where('activity_id',$activity_id)->find();
        $this->assign('id',$activity_id);
        $this->assign('res',$info);
        return $this->fetch();
    }

    /**
     * @return mixed
     * 详情
     */
    public function detail(){
        $activity_id = input('param.activity_id');
        $info = Db::table('up_activity')->where('activity_id',$activity_id)->find();
        $this->assign('res',$info);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('activity')->where('activity_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    /***
     * @return \think\response\Json
     * 上架
     */
    public function up(){
        $task_id = input('param.id');
        $res = Db::name('activity')->where('activity_id',$task_id)->setField('activity_type',1);
        if($res){
            return json(['code'=>200,'msg'=>'上架成功']);
        }else{
            return json(['code'=>100,'msg'=>'上架失败']);
        }
    }
    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('activity')->where('activity_id',$task_id)->setField('activity_type',2);
        if($res){
            return json(['code'=>200,'msg'=>'下架成功']);
        }else{
            return json(['code'=>100,'msg'=>'下架失败']);
        }
    }
}